﻿using System;
using System.Web;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Report
{
    public partial class ByProvinceByCatByDeliveryDate : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Check user permission
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            if (!Page.IsPostBack)
            {
                this.FillInReportDropdown_Adjust();

                //attach client events
                btnExport.Attributes.Add("onClick",
                                         "RptExportToExcel('" + ReportHelper.ReportType.DelayReport.ToString() +
                                         "');return false;");
                btnExport.Visible = false;

                RunReport(user.EmployeeId, int.Parse(ddlAdjust.SelectedItem.Value));
            }
            
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            RunReport(System.Convert.ToInt32(user.EmployeeId), int.Parse(ddlAdjust.SelectedItem.Value));
        }

        private void RunReport(long employeeID,int adjust)
        {
            string err = string.Empty;
            string reportHTML = string.Empty;
            string excelHTML = string.Empty;
            string rptTitle = string.Empty;
            try
            {
                reportHTML = BEGDC.PreContract.BLL.ReportHelper.GetReportByDeliveryDate(employeeID,
                                                                                   ReportHelper.ReportType.ByProvByCATByDelivery,
                                                                                   adjust,
                                                                                   out err,
                                                                                   out excelHTML,
                                                                                   out rptTitle);

                if (err != string.Empty)
                {
                    LabelReport_Quarterly.Text = "";
                    divError.InnerHtml = err;
                    btnExport.Visible = false;
                }
                else
                {
                    LabelReport_Quarterly.Text = rptTitle;
                    ScrollReport1.ContentHtml = reportHTML;
                    ScrollReport1.InitControl();
                    //divReport.InnerHtml = reportHTML;
                    Session[ReportHelper.ReportType.DelayReport.ToString()] = excelHTML;
                    divError.InnerHtml = "";
                    btnExport.Visible = true;
                }
            }
            catch(Exception exception)
            {
                LabelReport_Quarterly.Text = "";
                btnExport.Visible = false;
                divError.InnerHtml = AlertMessage.GetReportDataError;
                //this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + AlertMessage.GetReportDataError  + "')</script>");
            }
        }

        private void FillInReportDropdown_Adjust()
        {
            BaseFunction.BindContractAdjustDropDownList(ddlAdjust, string.Empty, true);
        }
    }
}